package cn.caiyj.algorithm.sort;

import cn.caiyj.algorithm.util.BaseUtil;
import com.alibaba.fastjson.JSON;

/**
 * @Description:插入排序:给插入元素腾出空间，将其余所有元素在插入之前向右移动一位
 * @author:CAIYJ
 * @Date:2018/10/24 21:48
 */
public class Insertion {

    public static void sort(Comparable[] a, boolean print) {
        int n = a.length;
        for (int i = 1; i < n; i++) {
            for (int j = i; j > 0 && BaseUtil.less(a[j], a[j - 1]); j--) {
                if (print) {
                    System.out.println(JSON.toJSONString(a));
                }
                BaseUtil.exch(a, j, j - 1);
            }
        }
    }
}
